//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2005-2006
//
// Object properties dialog - 'Raster map' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "RgnProperties.h"
# include "PropertyPages.h"
# include "Map.h"
# include "MapView.h"
# include "Scale.h"
# include "Position.h"
# include "Selection.h"
# include "PrintUnits.h"
# include "Static.h"
# include "fast_floor.h"
# include "Globals.h"

INT_PTR CALLBACK _RasterProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam);

void ShowRasterProperties (HWND _hWnd, const find_info_t & _Selection) {
	if (! _Selection.pRenderer)
		return;

	CCritSec::CLock lock (g_cs);

	// Prepare pages.
	const size_t c_cPages = 2;
	PROPSHEETPAGE pshp [c_cPages];
	::memset (pshp, 0, sizeof (pshp));
	size_t cPage;
	for (cPage = 0; cPage < c_cPages; ++ cPage) {
		pshp [cPage].dwSize    = sizeof (PROPSHEETPAGE);
		pshp [cPage].dwFlags   = PSP_DEFAULT;
		pshp [cPage].lParam    = reinterpret_cast<LPARAM> (_Selection.pRenderer);
		pshp [cPage].hInstance = g_hResInst;
	}
	cPage = 0;

	assert (cPage < c_cPages);
	pshp [cPage].pszTemplate = MAKEINTRESOURCE (IDD_RASTER);
	pshp [cPage].pfnDlgProc  = _RasterProc;
	++ cPage;

	// Prepare header.
	PROPSHEETHEADER psh;
	::memset (& psh, 0, sizeof (psh));
	psh.dwSize     = sizeof (psh);
	psh.dwFlags    = PSH_PROPSHEETPAGE;
	psh.hwndParent = g_hWnd;
	psh.hInstance  = g_hResInst;
	psh.nPages     = cPage;
	psh.nStartPage = 0;
	psh.ppsp       = pshp;
	psh.pszCaption = GetStr (IDS_IMAGE_PROPERTIES);

	::PropertySheet (& psh);
}

static
INT_PTR CALLBACK _RasterProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam) {
	renderer_base_t * const pRenderer = reinterpret_cast<renderer_base_t *> (::GetWindowLong (_hWnd, GWL_USERDATA));

	switch (_uMsg) {
		case WM_INITDIALOG: {
			CenterPropertyPages (_hWnd);

			//
			// Prepare access to data.
			//

			const PROPSHEETPAGE * const pPage = reinterpret_cast<PROPSHEETPAGE *> (_lParam);
			const renderer_base_t * const pRenderer = reinterpret_cast<const renderer_base_t *> (pPage->lParam);
			::SetWindowLong (_hWnd, GWL_USERDATA, pPage->lParam);

			// File name.
			::SetDlgItemText (_hWnd, IDC_FILE_NAME, pRenderer->strFileName.c_str ());

			// Width.
			::SetDlgItemInt (_hWnd, IDC_WIDTH,  pRenderer->Width  (), FALSE);
			// Height.
			::SetDlgItemInt (_hWnd, IDC_HEIGHT, pRenderer->Height (), FALSE);

			// Resolution.
			const long lH = pRenderer->Height ();
			const float fMetersPerPixel = pRenderer->rect.Height ()*40e6/360./(lH != 0 ? lH : 1);
			char strResolution [32], strResolutionUnits [16];
			PrintDistance (strResolution, strResolutionUnits, fMetersPerPixel);
			SetDlgItemTextF (_hWnd, IDC_RESOLUTION, "%s %s/pixel", strResolution, strResolutionUnits);

			return TRUE;
		}

		case WM_COMMAND: {
			switch (LOWORD (_wParam)) {
				case IDC_SHOW_ON_MAP: {
					UnselectObjects ();

					find_info_t fi;
					fi.pLayer    = NULL;
					fi.pRenderer = pRenderer;
					g_Selection.push_back (fi);

					SetScale (GetRectScale (pRenderer->rect), false);
					SetToCenter (point_t (pRenderer->rect.MeanX (), pRenderer->rect.MeanY ()));

					Refresh ();

					break;
				}
			}
			break;
		}
	}

	return FALSE;
}
